package com.cos.hospital.ui.record.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cos.hospital.R;
import com.cos.hospital.model.bean.AcupointBean;
import com.cos.hospital.model.bean.ImageBean;
import com.cos.hospital.model.bean.RecordWebBean;
import com.cos.hospital.tool.UDate;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.tool.glide.UGlide;
import com.cos.hospital.ui.record.RecordCode;
import com.cos.hospital.ui.record.bean.BeanRecord;
import com.cos.hospital.widget.EasySwipeMenuLayout;
import com.cos.hospital.widget.MyGridLayoutManager;
import com.cos.hospital.widget.TreatmentEvaluateView;
import com.cos.hospital.widget.VisionSimulationView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年09月18日 20:59
 * @Email: KosmoSakura@foxmail.com
 */
public class AdapterRecord extends BaseMultiItemQuickAdapter<BeanRecord, BaseViewHolder> {
    private boolean editable;
    List<AcupointBean> patientTreatmentInfoAcupoint = new ArrayList<>();

    public AdapterRecord(@Nullable List<BeanRecord> data, boolean editable) {
        super(data);
        this.editable = editable;
//        addItemType(RecordCode.Type.BaseInfo, R.layout.lay_rec_info);//基本信息
        addItemType(RecordCode.Type.ChiefDescribe, R.layout.item_rec_chief);//主诉
        addItemType(RecordCode.Type.CurrentMedicalHistory, R.layout.item_rec_chief);//现病史
        addItemType(RecordCode.Type.PastHistory, R.layout.item_rec_chief);//既往史
        addItemType(RecordCode.Type.AllergyHistory, R.layout.item_rec_chief);//过敏史
        addItemType(RecordCode.Type.MenstruationHistory, R.layout.item_rec_mest);//经产史
        addItemType(RecordCode.Type.TonguePulse, R.layout.item_rec_tongue);//舌脉象
        addItemType(RecordCode.Type.Diagnosis, R.layout.item_rec_chief);//辩证诊断
        addItemType(RecordCode.Type.Prescription, R.layout.item_rec_tongue);//治疗处方
        addItemType(RecordCode.Type.AcupunctureRecord, R.layout.item_acupuncture);//针灸记录
        addItemType(RecordCode.Type.FirstOrReTreament, R.layout.lay_treatment_type);//初复诊
    }

    @Override
    protected void convert(BaseViewHolder helper, BeanRecord item) {
        if (helper.getItemViewType() == RecordCode.Type.FirstOrReTreament) {
            RadioGroup rg = helper.getView(R.id.rg);
            LinearLayout all_bottom = helper.getView(R.id.all_bottom);
            VisionSimulationView vision_mulation = helper.getView(R.id.vision_mulation);
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (i) {
                        case R.id.rb_first_treatment:
                            all_bottom.setVisibility(View.GONE);
                            break;
                        case R.id.rb_re_treatment:
                            all_bottom.setVisibility(View.VISIBLE);
                            break;
                    }
                }
            });
            SeekBar remain_progress = vision_mulation.findViewById(R.id.remain_progress);
            TextView tv_num = vision_mulation.findViewById(R.id.tv_num);
            remain_progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    tv_num.setText(i + "/" + "100");
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
        } else {
            ImageView iv_edit = helper.getView(R.id.rec_edit);
            if (editable) {
                iv_edit.setVisibility(View.VISIBLE);
            } else {
                iv_edit.setVisibility(View.GONE);
            }
            EasySwipeMenuLayout esl = helper.getView(R.id.rec_esl);
            esl.setCanLeftSwipe(editable);
            helper.getView(R.id.rec_content).setOnClickListener(v -> esl.resetStatus());
            helper.addOnClickListener(R.id.rec_right);
            helper.addOnClickListener(R.id.rec_edit);
            if (helper.getItemViewType() == RecordCode.Type.AcupunctureRecord && helper.getView(R.id.other_treat_label) != null) {
                helper.getView(R.id.other_treat_label).setVisibility(View.VISIBLE);
                helper.getView(R.id.other_treat).setVisibility(View.VISIBLE);
            } else if (helper.getView(R.id.other_treat_label) != null) {
                helper.getView(R.id.other_treat_label).setVisibility(View.GONE);
                helper.getView(R.id.other_treat).setVisibility(View.GONE);

            }
        }
        switch (helper.getItemViewType()) {
            case RecordCode.Type.AcupunctureRecord://针灸记录:
                acupuncture(helper, item);
                helper.setText(R.id.other_treat, TextUtils.isEmpty(item.getFldTreatmentDesc()) ? "无" : item.getFldTreatmentDesc());
                break;
            case RecordCode.Type.ChiefDescribe://主诉:
            case RecordCode.Type.CurrentMedicalHistory://现病史:
            case RecordCode.Type.PastHistory://既往史:
            case RecordCode.Type.AllergyHistory://过敏史:
            case RecordCode.Type.Diagnosis://辩证诊断:
                inclouldNormalData(helper, item);
                break;
            case RecordCode.Type.MenstruationHistory://经产史:
                inclouldMensData(helper, item);
                break;
            case RecordCode.Type.TonguePulse://舌脉象:
            case RecordCode.Type.Prescription://治疗处方:
                inclouldTnggueData(helper, item);
                break;
        }
    }

    public void setPatientTreatmentInfoAcupoint(List<AcupointBean> patientTreatmentInfoAcupoint) {
        this.patientTreatmentInfoAcupoint = patientTreatmentInfoAcupoint;
    }

    private void acupuncture(BaseViewHolder helper, BeanRecord item) {
        helper.setText(R.id.rec_cif_tag, item.getFldElementName());

        RecyclerView rc_acupoint = helper.getView(R.id.rc_acupoint);
        AdapterAcupoint adapterAcupoint = new AdapterAcupoint(patientTreatmentInfoAcupoint);
        MyGridLayoutManager myGridLayoutManager = new MyGridLayoutManager(mContext, 4);
        myGridLayoutManager.setScrollEnabled(false);
        rc_acupoint.setLayoutManager(myGridLayoutManager);
        rc_acupoint.setAdapter(adapterAcupoint);
    }

    /**
     * @apiNote 主述、现病史、既往史、过敏史、辩证诊断、
     */
    private void inclouldNormalData(BaseViewHolder helper, BeanRecord item) {
        helper.setText(R.id.rec_cif_tag, item.getFldElementName());
        TextView tDiag = helper.getView(R.id.rec_cif_txt);
        tDiag.setHint("添加" + item.getFldElementName());
        tDiag.setText(UTxt.isNull(item.getFldElementValue()));
    }

    /**
     * @apiNote 舌脉象、治疗处方
     */
    private void inclouldTnggueData(BaseViewHolder helper, BeanRecord tongueBean) {
        helper.setText(R.id.rec_mest_tag, tongueBean.getFldElementName());
        TextView tongue = helper.getView(R.id.rec_mest_txt);
        tongue.setHint("添加" + tongueBean.getFldElementName());
        tongue.setText(UTxt.isNull(tongueBean.getFldElementValue()));
        List<ImageBean> imageBeanList = tongueBean.getImageBeanList();
        int tongueSize = UTxt.getSize(imageBeanList);
        View i_line = helper.getView(R.id.rec_tongue_line);
        ImageView i_1 = helper.getView(R.id.rec_tongue_img_1);
        ImageView i_2 = helper.getView(R.id.rec_tongue_img_2);
        ImageView i_3 = helper.getView(R.id.rec_tongue_img_3);
        helper.addOnClickListener(R.id.rec_tongue_img_1);
        helper.addOnClickListener(R.id.rec_tongue_img_2);
        helper.addOnClickListener(R.id.rec_tongue_img_3);
        if (tongueSize < 1) {
            i_line.setVisibility(View.GONE);
            i_1.setVisibility(View.GONE);
            i_2.setVisibility(View.GONE);
            i_3.setVisibility(View.GONE);
        } else if (tongueSize == 1) {
            i_line.setVisibility(View.VISIBLE);
            i_1.setVisibility(View.VISIBLE);
            i_2.setVisibility(View.GONE);
            i_3.setVisibility(View.GONE);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(0).getFldImgPath(), i_1);
        } else if (tongueSize == 2) {
            i_line.setVisibility(View.VISIBLE);
            i_1.setVisibility(View.VISIBLE);
            i_2.setVisibility(View.VISIBLE);
            i_3.setVisibility(View.GONE);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(0).getFldImgPath(), i_1);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(1).getFldImgPath(), i_2);
        } else {
            i_line.setVisibility(View.VISIBLE);
            i_1.setVisibility(View.VISIBLE);
            i_2.setVisibility(View.VISIBLE);
            i_3.setVisibility(View.VISIBLE);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(0).getFldImgPath(), i_1);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(1).getFldImgPath(), i_2);
            UGlide.loadRound(helper.itemView.getContext(), imageBeanList.get(2).getFldImgPath(), i_3);
        }
    }

    /**
     * @apiNote 月经史
     */
    private void inclouldMensData(BaseViewHolder helper, BeanRecord item) {
        EasySwipeMenuLayout mlp = helper.getView(R.id.lmp_esl);
        EasySwipeMenuLayout gp = helper.getView(R.id.gp_esl);
        EasySwipeMenuLayout br = helper.getView(R.id.br_esl);
        mlp.setCanLeftSwipe(editable);
        gp.setCanLeftSwipe(editable);
        br.setCanLeftSwipe(editable);
        helper.getView(R.id.formula_lmp_root).setOnClickListener(v -> mlp.resetStatus());
        helper.getView(R.id.formula_gp_root).setOnClickListener(v -> gp.resetStatus());
        helper.getView(R.id.formula_br_root).setOnClickListener(v -> br.resetStatus());
        helper.addOnClickListener(R.id.lmp_right);
        helper.addOnClickListener(R.id.gp_right);
        helper.addOnClickListener(R.id.br_right);
        helper.setText(R.id.rec_mest_tag, item.getFldElementName());
        TextView tDiag = helper.getView(R.id.rec_mest_txt);
        tDiag.setHint("添加经产史");
        tDiag.setText(UTxt.isNull(item.getFldElementValue()));
        RecordWebBean.MhfBean mhfBean = item.getMhfBean();
        RecordWebBean.RhfBean rhfBean = item.getRhfBean();

        //月经史公式
        if (mhfBean != null) {
            mlp.setVisibility(View.VISIBLE);
            helper.setText(R.id.formula_lmp_first, mhfBean.getFldMenarcheAge() + "岁");
            helper.setText(R.id.formula_lmp_long, mhfBean.getFldMinPeriod() + " - " + mhfBean.getFldMaxPeriod() + " 天");
            helper.setText(R.id.formula_lmp_cycle, mhfBean.getFldMinCycle() + " - " + mhfBean.getFldMaxCycle() + " 天");
            //公式类型(0以年龄,1日期)
            if (mhfBean.getFldType() == 1) {
                helper.setText(R.id.formula_lmp_end, UDate.strToStrSafety(mhfBean.getFldLmp(), "yyyy-MM-dd"));
            } else {
                helper.setText(R.id.formula_lmp_end, mhfBean.getFldMenopauseAge() + "岁");
            }
        } else {
            mlp.setVisibility(View.GONE);
        }
        //生育史公式
        if (rhfBean != null) {
            //生育史
            if (rhfBean.getFldType() == 1) {
                br.setVisibility(View.VISIBLE);//生育史
                gp.setVisibility(View.GONE);//孕产史
                helper.setText(R.id.formula_br_full, rhfBean.getFldTermDelivery() + "次");
                helper.setText(R.id.formula_br_early, rhfBean.getFldPrematureDelivery() + "次");
                helper.setText(R.id.formula_br_fail, rhfBean.getFldAbortion() + "次");
                helper.setText(R.id.formula_br_alive, rhfBean.getFldChildrenNum() + "次");
            } else {
                gp.setVisibility(View.VISIBLE);//孕产史
                br.setVisibility(View.GONE);//生育史
                helper.setText(R.id.formula_gp_g, rhfBean.getFldPregnancy() + "次");
                helper.setText(R.id.formula_gp_plost, rhfBean.getFldParturition() + "次");
                helper.setText(R.id.formula_gp_mark, UTxt.isNull(rhfBean.getFldDesc()));
            }
        } else {
            gp.setVisibility(View.GONE);//孕产史
            br.setVisibility(View.GONE);//生育史
        }
    }


}
